/*global require*/
'use strict';

// Require.js allows us to configure shortcut alias
require.config({
	// The shim config allows us to configure dependencies for
	// scripts that do not call define() to register a module
	shim: {
		backbone: {
			deps: [
				'underscore',
				'jquery'
			],
			exports: 'Backbone'
		},
		backboneLocalstorage: {
			deps: ['backbone'],
			exports: 'Store'
		},
		bootstrap: {
			deps: [
					'jquery'
				],
			exports: 'Bootstrap'
		}
	},
	
	hbs : {
		templateExtension : 'html',
        // if disableI18n is `true` it won't load locales and the i18n helper
        // won't work as well.
		disableI18n : true
    },
	paths: {
		jquery: 'js/jquery/jquery',
		hbs: 'js/hbs/hbs',
		handlebars: 'js/handlebars/handlebars-1.0.rc.2',
		json2 : 'js/json2/json2',
		i18nprecompile : 'js/i18nprecompile/i18nprecompile',
		backbone: 'js/backbone/backbone',
		underscore: 'js/underscore/underscore',
		backboneLocalstorage: 'js/backbonebackbone.localStorage'
	}
});

require([
    'jquery',
	'backbone',
	'routers/router',
	'views/main',
	'models/main',
	'views/book',
	'models/book',
	'views/books',
	'collections/books',
	'views/home',
	'views/menu',
], function ($, Backbone) {
	// Init router
	var router = new Workspace();
	window.booksCollection = new BooksCollection();
	var sortConfig = {attrs: [{attr: "name", name:"Name", asc:false}, {attr: "price", name:"Price", asc:false}]};
	
	window.booksCollection.sortConfig = sortConfig;
	window.booksView = new BooksView({
		model: window.booksCollection
	});
	var initApp = function(page, options) {
		var mainModel = new MainModel({page:page});
		new MainView({
			model: mainModel,
			el: $("#main")
		});
		mainModel.trigger('change:page');
		
		var menusModel = new Backbone.Model({
			menus: [{menuId:"m1",active: false, link:"#home", value:"Home", page:"home", className:"menu-home"},
			        {menuId:"m2",active: false, link:"#books/list", value:"List", page:"books", className:"menu-list"},
			        {menuId:"m3",active: false, link:"#books/tiles", value:"Tiles", page:"books", className:"menu-tiles"}],
		});
		new MenuView({
			el: $('#menu'),
			model: menusModel
		});
		
		var menus = menusModel.get('menus');
		
		if (page == "home") {
			var homeModel = new Backbone.Model();
			new HomeView({
				model: homeModel,
				el: $('#home')
			});
			homeModel.trigger('change');
			menus[0].active = true;
		} else if (page == "books") {
			var viewType = options.type;
			var pageId = options.pageId;
			var maxItemOfPage = 5;
			if (!viewType) {
				viewType = "list";
			}
			
			if (!pageId) {
				pageId = 1;
			}
			
			if (viewType=="list") {
				menus[1].active = true;
			} else {
				menus[2].active = true;
				maxItemOfPage = 8;
			}
			window.booksCollection.maxItemOfPage = maxItemOfPage;
			
			window.booksCollection.pageId = pageId;
			window.booksCollection.viewType = viewType;
			window.booksView.$el = $("#books");
			window.booksCollection.fetch({reset: true, url:'price.json'});
		}
		menusModel.trigger('change');
	};
	router.on('route:home', function(){
		initApp('home');
	});
	
	router.on('route:books', function(type, pageId){
		initApp('books', {type: type, pageId: pageId});
	});
	
	router.on('route:defaultPage', function(type, pageId){
		initApp('books', {type: type, pageId: pageId});
	});
	
	router.on('route:defaultType', function(type, pageId){
		initApp('books', {type: type, pageId: pageId});
	});
	
	Backbone.history.start();
});
